package com.ktg.mes.chanjet.task;

import cn.hutool.json.JSONObject;
import com.ktg.common.exception.BussinessException;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.chanjet.content.InventoryClassContent;
import com.ktg.mes.chanjet.response.InventoryClassResponse;
import com.ktg.mes.chanjet.spi.ChanjetSpi;
import com.ktg.mes.md.domain.ItemType;
import com.ktg.mes.md.service.IItemTypeService;
import com.ktg.mes.wm.domain.WmMaterialStock;
import com.ktg.mes.wm.service.IWmMaterialStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * T+ 仓库存货同步
 */

@Slf4j
@Component("itemStockSyncDataTask")
public class ItemStockSyncDataTask {

    @Autowired
    IWmMaterialStockService iWmMaterialStockService;
    @Autowired
    ChanjetSpi chanjetSpi;


    public void execute(){
        try{
            InventoryClassContent content = new InventoryClassContent();
            JSONObject groupInfo = new JSONObject();
            JSONObject isBatch = new JSONObject();
            isBatch.putOpt("IsBatch",true);
            groupInfo.putOpt("GroupInfo",isBatch);
            content.setParam(groupInfo);
            iWmMaterialStockService.cleanData();
            InventoryClassResponse response = chanjetSpi.getStockList(content);
            List<WmMaterialStock> stocks = new ArrayList<>();
            List<Map> stockListResponse = response.getData();
            for (Map map:stockListResponse) {
                WmMaterialStock stock = new WmMaterialStock();
                stock.setItemId(new BigDecimal((String)map.get("InventoryID")).longValue());
                stock.setItemName((String)map.get("InventoryName"));
                stock.setItemCode((String)map.get("InventoryCode"));
                stock.setItemType((String)map.get("InventoryClassName"));
                if(map.get("WarehouseID") !=null && !map.get("WarehouseID").equals("")){
                    stock.setWarehouseId(new BigDecimal((String)map.get("WarehouseID")).longValue());
                    stock.setWarehouseCode((String)map.get("WarehouseCode"));
                    stock.setWarehouseName((String)map.get("WarehouseName"));
                }
                stock.setBatchCode((String)map.get("Batch"));
                stock.setQuantityOnhand(new BigDecimal((String) map.get("AvailableQuantity")));
                stock.setExistingQuantity(new BigDecimal((String) map.get("ExistingQuantity")));
                stock.setLocationName((String)map.get("InvLocationName"));
                stock.setLocationCode((String)map.get("InvLocationCode"));
                stock.setUnitOfMeasure((String)map.get("UnitName"));
                stock.setUpdateTime(new Date());
                stocks.add(stock);
            }
            iWmMaterialStockService.saveOrUpdateBatch(stocks);
        }catch (Exception ex){
            log.error("同步产品库存 信息出错",ex);
            throw new BussinessException("执行失败");
        }
    }
}
